package com.lai.shop.controller;

import com.lai.shop.service.CartService;
import com.lai.shop.utils.R;
import com.lai.shop.vo.AddProductToCartVo;
import com.lai.shop.vo.CartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/cart")
@Api(value = "购物车模块",tags = "购物车模块")
public class CartController {

    @Autowired
    CartService cartService;

    @ApiOperation(value = "获取购物车列表")
    @GetMapping("/cart.html")
    public String cartList(Model model){
        CartVo cartVo = cartService.cartList();
        model.addAttribute("cart",cartVo);
        return "shop_car";
    }


    @ApiOperation(value = "添加商品到购物车")
    @PostMapping("/addProductToCart")
    public String addProductToCart(@RequestBody AddProductToCartVo addProductToCartVo){
        cartService.addProductToCart(addProductToCartVo);
        return "redirect:http://localhost:8099/cart/cart.html";
    }


    @ApiOperation(value = "添加商品到购物车")
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("productId") Long productId){
        cartService.deleteItem(productId);
        return "redirect:http://localhost:8099/cart/cart.html";
    }


    @ApiOperation(value = "清空购物车")
    @GetMapping("/clearCart")
    public String deleteCart(){
        cartService.clearCart();
        return "redirect:http://localhost:8099/cart/cart.html";
    }

    @ApiOperation(value = "清除选中的购物项")
    @GetMapping("/clearCheckCartItem")
    public String clearCheckCartItem(){
        cartService.clearCheckCartItem();
        return "redirect:http://localhost:8099/cart/cart.html";
    }

    @ApiOperation(value = "全选")
    @GetMapping("/allCheck")
    public String allCheck(){
        cartService.allCheck();
        return "redirect:http://localhost:8099/cart/cart.html";
    }


    @ApiOperation(value = "取消/选中购物项")
    @GetMapping("/checkItem")
    @ResponseBody
    public R checkItem(@RequestParam("productId") Long productId,@RequestParam("check") Integer check){
        CartVo cartVo = cartService.checkItem(productId,check);
        return R.ok().put("data",cartVo);
    }


    @ApiOperation(value = "计算购物项数量")
    @GetMapping("/countItem")
    @ResponseBody
    public R countItem(@RequestParam("productId") Long productId,@RequestParam("num") Integer num){
        CartVo cartVo = cartService.countItem(productId,num);
        return R.ok().put("data",cartVo);
    }




}
